package com.champrin.replicationofmyblockapix.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import com.champrin.replicationofmyblockapix.BlockAPixView;
import com.champrin.replicationofmyblockapix.R;

public class GameActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        // 游戏视图
        BlockAPixView blockAPixView = findViewById(R.id.block_a_pix_view);

        // 返回菜单按钮
        Button menuBtn = findViewById(R.id.menu_btn);
        menuBtn.setOnClickListener(v -> {
            // 返回ActivityMenu
//            Intent returnIntent = new Intent();
//            returnIntent.putExtra("result", "Returned from GameActivity!");
//            setResult(RESULT_OK, returnIntent);
            blockAPixView.setBackToMenu(true);
            finish();
        });

        // 保存存档按钮
        Button saveDataBtn = findViewById(R.id.save_data_btn);
        saveDataBtn.setOnClickListener(v -> {
            // 保存游戏数据
            TransformSaveGameData(blockAPixView.TransformGridsSaveData(), blockAPixView.TransformChunksSaveData());
        });

        // 存为图片按钮
        Button toImageBtn = findViewById(R.id.to_image_btn);
        toImageBtn.setOnClickListener(v -> {
            // 保存游戏数据
            saveBitmapToPhotoFile(blockAPixView.saveBitmap());
        });

        // 视图消息传递 message == create为新游戏 continue为继续游戏
        String message = getIntent().getStringExtra("message");
        int level = getIntent().getIntExtra("level", 999);
        if (message.equals("create")) {
            blockAPixView.setCreateBySQL(level);
//            blockAPixView.setRandomSpawn();
        } else {
            SharedPreferences spFile = getSharedPreferences(
                    getResources().getString(R.string.block_a_pix_game_data_file),
                    Context.MODE_PRIVATE);

            String saveGameData = spFile.getString(getResources().getString(R.string.game_data_key), "unknown");
            String chunkDataString = spFile.getString(getResources().getString(R.string.game_chunks_data_key), "unknown");

            Log.v("ACTION", "read " + saveGameData);
            Log.v("ACTION", "read " + chunkDataString);

            if (saveGameData.equals("unknown")) {
                Toast.makeText(this, "save data unknown", Toast.LENGTH_LONG).show();
            } else {
                blockAPixView.setContinueGame(saveGameData, chunkDataString);
            }
        }
    }

    public void TransformSaveGameData(String dataString, String chunkDataString) {
        SharedPreferences spFile = getSharedPreferences(
                getResources().getString(R.string.block_a_pix_game_data_file),
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = spFile.edit();

        editor.putString(getResources().getString(R.string.game_data_key), dataString);
        editor.putString(getResources().getString(R.string.game_chunks_data_key), chunkDataString);
        editor.apply();
    }

    // 保存Bitmap到相册
    public void saveBitmapToPhotoFile(Bitmap bitmap) {
        // 获取保存路径
        String savedImagePath = MediaStore.Images.Media.insertImage(
                getContentResolver(),
                bitmap,
                "Image",
                "Description"
        );

        // 通知相册更新
        Uri savedImageURI = Uri.parse(savedImagePath);
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(savedImageURI);
        sendBroadcast(intent);
    }

}